package motionblur.control;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

import motionblur.Ball;
import motionblur.Settings;

public class KeyControl implements KeyListener {
	
	private List<Ball> balls;
	
	public KeyControl(List<Ball> balls) {
		super();
		this.balls = balls;
	}

	public void keyPressed(KeyEvent e) {
		switch(e.getKeyCode()) {
		//ESC: quit program
		case KeyEvent.VK_ESCAPE: {
			System.exit(0);
		}
		//SPACE: add a ball
		case KeyEvent.VK_SPACE: {
			moreBalls();
			break;
		}
		//BACKSPACE: remove the last ball
		case KeyEvent.VK_BACK_SPACE: {
			lessBalls();
			break;
		}
		//F1: show settings
		case KeyEvent.VK_F1: {
			Settings.getInstance().setShowInfos(!Settings.getInstance().isShowInfos());
		}
		}
		
	}
	
	public void keyReleased(KeyEvent e) {}
	
	public void keyTyped(KeyEvent e) {}

	private void moreBalls() {
		synchronized(balls) {
			balls.add(new Ball(100, 100, 100, 100));
		}	
	}
	
	private void lessBalls() {
		if(balls.size() - 1 >= 0) {
			synchronized(balls) {
				balls.remove(balls.size() - 1);
			}	
		}		
	}
}
